#! /usr/bin/env perl

use strict;
use warnings;

use Cwd qw(getcwd);

use VCtools::Base;
use VCtools::Args;
use VCtools::Common;


#################################
# OPTIONS AND ARGUMENTS
#################################

VCtools::args('project', 'single', 'project to build');
VCtools::getopts();

my $project = VCtools::project();
print STDERR "vbuild: going to try to build project $project\n" if DEBUG >= 4;


#################################
# CHECK FOR ERRORS
#################################

print STDERR "vbuild: working dir is " . VCtools::WORKING_DIR . "\n" if DEBUG >= 3;
VCtools::fatal_error("must be run from top-level personal testing directory") unless VCtools::in_working_dir();
unless (VCtools::proj_exists_in_vc($project))
{
	my $msg = "project $project does not exist in VC";
	$msg .= " (server path: " . VCtools::_project_path($project) . ")" if VCtools::verbose();
	VCtools::fatal_error($msg);
}
VCtools::fatal_error("project $project already exists") if -e $project;

# check for proper group
VCtools::verify_gid($project);


#################################
# MAIN
#################################

# leaving second argument blank indicates we want the entire project
VCtools::get_tree($project, "", $project);

# check for post-build script and execute it
foreach (VCtools::project_script($project, 'PostBuild'))
{
	print "$_\n" if VCtools::verbose();
	system($_) == 0
			or VCtools::fatal_error("script line failed; check config file");
}

print "done\n" unless $?;


#################################
# SUBS
#################################
